package com.huiztech.kaoqinji;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle.Control;

import com.huiztech.kaoqinji.model.Remark;
import com.huiztech.kaoqinji.model.WXMessage;
import com.huiztech.kaoqinji.view.SwipeListView;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.util.LogUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

public class RemarkListActivity extends BaseActivity {
    private List<Remark> data = new ArrayList<Remark>();;
    private SwipeListView mListView;
    SwipeAdapter mAdapter ;
    ImageView addImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remarklist);
        mListView = (SwipeListView) findViewById(R.id.listview);
        addImage=(ImageView) findViewById(R.id.add_remark_text);

        LogUtils.i(data+"+");
        LogUtils.i(controller.getRemarksFromDB()+"==");
        data=controller.getRemarksFromDB();
            initView();
        addImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(RemarkListActivity.this, AddRemarkActivity.class);
                intent.putExtra("id",0);
                startActivity(intent);
            }
        });
    }

    private void initView() {
         mAdapter = new SwipeAdapter(this, data, mListView.getRightViewWidth());
        mAdapter.setOnRightItemClickListener(new SwipeAdapter.onRightItemClickListener() {

            @Override
            public void onRightItemClick(View v, int position) {
                controller.delRemarkFromDbById(data.get(position).getId());
                data.remove(position);
                mListView.hiddenRight(mListView.mPreItemView);
                mAdapter.notifyDataSetChanged();
                Toast.makeText(RemarkListActivity.this, R.string.delete_remark_success, Toast.LENGTH_SHORT).show();
            }
        });

        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(RemarkListActivity.this, AddRemarkActivity.class);
                intent.putExtra("id", data.get(position).getId());
                intent.putExtra("content", data.get(position).getContent());
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(data!=null){
            data.clear();

        }
        data.addAll(controller.getRemarksFromDB());
        mAdapter.notifyDataSetChanged();

    }
}
